<?php
namespace app\admin\controller;
use think\Controller;
use think\Validate;

class Module extends Base
{
	//广告管理
	public function ad()
	{
		$a=model('Ad');
		$res=$a->show()->paginate([
			'list_rows'=>5,
			'query'=>input('get.'),
			'var_page'=>'p'
		]);
		$this->assign('res',$res);
		$cou=$a->cou();
		$this->assign('cou',$cou);
		return $this->fetch();
	}
	//广告管理添加
	public function adadd()
	{
		//dump(request()->file('image'));
		//文件上传操作
		if(request()->file('image')!==null)
		{
			$file=request()->file('image');
			//文件名
			$info=$file->move('./static/admin/ad/');
			//文件路径(有点):删除用
			$path="./static/admin/ad/".$info->getSaveName();
			//文件路径(无点):除删除，用
			$npath="/static/admin/ad/".$info->getSaveName();
			//如果最新文件与原图片不同，且传到后台的为文件
			if(input('img') != $path && is_file(input('img')))
			{
				//删除文件
				unlink(input('img'));
			}
			//将新文件的路径传至前台的隐藏域中存放，待点击‘添加’后传值操作
			return json_encode(['npath'=>$npath]);
		}
		if($this->request->isPost())
		{
			//dump(input());
			$result=$this->validate(input(),'Ad');
			if($result===true)
			{
				$time=time();
				$arr=[
					'name'=>input('post.name'),
					'desc'=>input('post.desc'),
					'address'=>input('post.tpath'),
					'time'=>$time,
				];
				model('Ad')->adda($arr);
				return $this->success('添加成功');
			}
			else
			{
				return $this->error($result);
			}
		}
		else
		{
			return $this->fetch();
		}
	}
	//广告管理修改
	public function adedit()
	{
		if($this->request->isPost())
		{
			//dump(input());
			$result=$this->validate(input(),'Ad');
			if($result===true)
			{
				$time=time();
				$arr=[
					'name'=>input('post.name'),
					'desc'=>input('post.desc'),
					'time'=>$time,
				];
				model('Ad')->updzd(input('post.id'),$arr);
				return $this->success('修改成功');
			}
			else
			{
				return $this->error($result);
			}
		}
		else
		{
			//dump(input('param.id'));
			$res=model('Ad')->selo(input('param.id'));
			$this->assign('res',$res);
			return $this->fetch();	
		}
		
	}
	//广告管理删除
	public function addel()
	{
		$id=input('post.id');
		if(input('post.allid'))
		{
			$id=substr(input('post.allid'),0,-1);
		}
		//dump($id);
      	$num = model('Ad')->count();
        if($num <= 1){
          return $this->error('至少展示一张');
        }
		$result=model('Ad')->delad($id);
		if($result===0)
		{
			return $this->error('删除失败');
		}
		else
		{
			return $this->success('删除成功');
		}
	}
	//友情链接管理
	public function fl()
	{
		$a=model('FriendLink');
		$res=$a->show()->paginate([
			'list_rows'=>5,
			'query'=>input('get.'),
			'var_page'=>'p'
		]);
		$total=model('FriendLink')->cou();
		$this->assign('res',$res);
		$this->assign('total',$total);
		return $this->fetch();
	}
	//友情链接添加
	public function fladd()
	{
		//dump(input());
		if($this->request->isPost())
		{
			$result=$this->validate(input(),'Fl');
			if($result===true)
			{
				$time=time();
				//dump($time);
				$arr=[
					'linkname'=>input('post.linkname'),
					'linkurl'=>input('post.linkurl'),
					'time'=>$time,
				];
				model('FriendLink')->addfl($arr);
				return $this->success('添加成功');
			}
			else
			{
				return $this->error($result);
			}
		}
		else
		{
			return $this->fetch();
		}
		
	}
	//友情链接修改
	public function fledit()
	{
		if($this->request->isPost())
		{
			$result=$this->validate(input(),'Fl');
			if($result===true)
			{
				$time=time();
				$id=input('post.id');
				$arr=[
					'linkname'=>input('post.linkname'),
					'linkurl'=>input('post.linkurl'),
					'time'=>$time,
				];
				model('FriendLink')->updfl($id,$arr);
				return $this->success('修改成功');
			}
			else
			{
				return $this->error($result);
			}
		}
		else
		{
			$res=model('FriendLink')->selfl(input('param.id'));
			$this->assign('res',$res);
			return $this->fetch();
		}
	}
	//友情链接删除
	public function fldel()
	{
		$id=input('post.id');
		if(input('post.allid'))
		{
			$id=substr(input('post.allid'),0,-1);
		}
		//dump($id);
		$result=model('FriendLink')->delfl($id);
		if($result===0)
		{
			return $this->error('删除失败');
		}
		else
		{
			return $this->success('删除成功');
		}
	}
	//新闻管理
	public function newscontent()
	{
		$a=model('NewsContent');

		$res=$a->show()->paginate([
			'list_rows'=>5,
			'query'=>input('get.'),
			'var_page'=>'p'
		]);
		//dump($res);
		$this->assign('res',$res);
		$cou=$a->cou();
		$this->assign('cou',$cou);
		return $this->fetch();
	}
	//新闻管理详情
	public function newscontentmore()
	{
		//dump(input('param.id'));
		$last=model('NewsContent')->selone(input('param.id'));
		//dump($last);
		$last[0]['allimg']=explode(',',$last[0]['allimg']);
		//dump($last);die;
		$this->assign('last',$last);
		$res1=model('NewsClassify')->sellev1();
		$res2=model('NewsClassify')->sellev2();
		$this->assign('res1',$res1);
		$this->assign('res2',$res2);
		return $this->fetch();
		
	}
	//新闻管理添加
	public function newscontentadd()
	{
		if(request()->file('image')!==null)
		{
			$file=request()->file('image');
			$info=$file->move('./static/admin/news/');
			$path="./static/admin/news/".$info->getSaveName();
			$npath="/static/admin/news/".$info->getSaveName();
			//dump($arr);
			return json_encode(['npath'=>$npath]);
		}
		if($this->request->isPost())
		{
			//dump(input());
			$result=$this->validate(input(),'NewsContent');
			if($result===true)
			{
				$allimg=implode(',',input('post.tpath'));
				//var_dump($allimg);
				//添加不完全，加8张数据库只加5张，可能为数据库问题
				$time=time();
				$arr=[
					'title'=>input('post.title'),
					'content'=>input('post.desc'),
					'allimg'=>$allimg,
					'parentid'=>input('post.id'),
					'time'=>$time,
				];
				model('NewsContent')->addnc($arr);
				return $this->success('添加成功');
			}
			else
			{
				return $this->error($result);
			}
		}
		else
		{
			$res1=model('NewsClassify')->sellev1();
			$res2=model('NewsClassify')->sellev2();
			$this->assign('res1',$res1);
			$this->assign('res2',$res2);
			return $this->fetch();
		}
		
	}
	//新闻管理修改
	public function newscontentedit()
	{
		return $this->fetch();
	}
	//新闻管理删除
	public function newscontentdel()
	{
		$id=input('post.id');
		if(input('post.allid'))
		{
			$id=substr(input('post.allid'),0,-1);
		}
		//dump($id);
		$result=model('NewsContent')->delnc($id);
		if($result===0)
		{
			return $this->error('删除失败');
		}
		else
		{
			return $this->success('删除成功');
		}
	}

	//新闻分类管理
	public function newsclassify()
	{
		$res=model('NewsClassify')->selnc();
		//dump($res);
		$this->assign('res',$res);
		$arr=model('NewsClassify')->selparid();
		$brr='';
		foreach($arr as $v)
		{
			$brr.=$v['parentid'].',';
		}
		$brr=substr($brr,0,-1);
		$brr=explode(',',$brr);
		$brr=array_unique($brr);
		$brr=implode(',',$brr);
		//dump($brr);
		$this->assign('brr',$brr);
		$cou=model('NewsClassify')->cou();
		$this->assign('cou',$cou);
		return $this->fetch();
	}
	//新闻分类管理添加
	public function newsclassifyadd()
	{
		if($this->request->isPost())
		{
			//dump(input());die;
			$result=$this->validate(input(),'NewsClassify');
			if($result===true)
			{
				$parentid='';
				$level='';
				//dump(input('post.parentid'));die;
				if(input('post.parentid')==='0')
				{
					//echo 1;die;
					$parentid=0;
					$level=1;
				}
				else
				{
					//echo 2;die;
					$parentid=input('post.parentid');
					$level=2;
				}
				$time=time();
				$arr=[
					'name'=>input('post.name'),
					'parentid'=>$parentid,
					'level'=>$level,
					'ncid'=>0,
					'time'=>$time,
				];
				model('NewsClassify')->addnc($arr);
				return $this->success('添加成功');
			}
			else
			{
				return $this->error($result);
			}
		}
		else
		{
			$opt=model('NewsClassify')->sellev1();
			$this->assign('opt',$opt);
			return $this->fetch();
		}
		
	}
	//新闻分类管理修改
	public function newsclassifyedit()
	{

		if($this->request->isPost())
		{
			$result=$this->validate(input(),'NewsClassify');
			if($result===true)
			{
				$parentid='';
				$level='';
				//dump(input('post.parentid'));die;
				if(input('post.parentid')==='0')
				{
					//echo 1;die;
					$parentid=0;
					$level=1;
				}
				else
				{
					//echo 2;die;
					$parentid=input('post.parentid');
					$level=2;
				}
				$time=time();
				$id=input('post.id');
				$arr=[
					'name'=>input('post.name'),
					'parentid'=>$parentid,
					'level'=>$level,
					'ncid'=>0,
					'time'=>$time,
				];
				model('NewsClassify')->updnc($id,$arr);
				return $this->success('修改成功');
			}
			else
			{
				return $this->error($result);
			}
		}
		else
		{
			//dump(input('param.id'));
			
			$arr=model('NewsClassify')->selparid();
			$brr='';
			foreach($arr as $v)
			{
				$brr.=$v['parentid'].',';
			}
			$brr=substr($brr,0,-1);
			$brr=explode(',',$brr);
			$brr=array_unique($brr);
			$brr=implode(',',$brr);
			//dump($brr);
			//brr为所有parentid的集合
			$this->assign('brr',$brr);

			$one=model('NewsClassify')->sel(input('param.id'));
			//dump($one[0]['id']);
			$this->assign('one',$one);
			$all=model('NewsClassify')->sellev1();
			$this->assign('all',$all);
			return $this->fetch();	
		}
		
	}
	//新闻分类管理删除
	public function newsclassifydel()
	{
		$id=input('post.id');
		if(input('post.allid'))
		{
			$id=substr(input('post.allid'),0,-1);
		}
		//dump($id);
		$result=model('NewsClassify')->delnc($id);
		if($result===0)
		{
			return $this->error('删除失败');
		}
		else
		{
			return $this->success('删除成功');
		}
	}
	//置顶
	public function up()
	{
		$all=model('Ad')->sela();
		//dump($all);
		$a='';
		foreach($all as $v)
		{
			$a.=$v['orderby'].',';
		}
		$a=substr($a,0,-1);
		$a=explode(',',$a);
		$a=max($a);
		//dump($a);
		$one=model('Ad')->selo(input('post.id'));
		$b=$one[0]['orderby'];
		$c='';
		if($b<=$a)
		{
			$c=$a+1;
		}
		else
		{
			$c=$b;
		}
		//dump($b);
		$arr=['orderby'=>$c];
		$res=model('Ad')->updzd(input('post.id'),$arr);
		if($res===1)
		{
			return $this->success('置顶成功');
		}
		else
		{
			return $this->error('置顶失败');
		}
		//dump($res);
	}
}